<?php
/**
 * Created by PhpStorm. 微信分享挂件
 * User: FDG367
 * Date: 2017/11/07
 * Time: 16:01
 */
namespace common\widgets;

use common\helps\redisHelper;
use common\helps\StringHelper;
use common\helps\tools;
use Yii;
use yii\bootstrap\Html;
use \yii\bootstrap\Widget;
use yii\helpers\ArrayHelper;
use yii\web\JsExpression;

class WechatShare extends Widget{
	public $url;
	public $title	='剪刀石头布家具';
	public $desc	='';
	public $link	= '';
	public $imgUrl	='';



	public function init(){
		parent::init();
	}

	public function run()
	{
		if(!tools::isWeixin()){
			return false;
		}
		$timestamp 		= time();
		$jsapiTicket	= redisHelper::getJsApiTicket();
		$nonceStr		= StringHelper::randString(16,16);

		$this->url = tools::getProtocol().$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

		//加载数据
		// 这里参数的顺序要按照 key 值 ASCII 码升序排序
		$string = "jsapi_ticket=$jsapiTicket->ticket&noncestr=$nonceStr&timestamp=$timestamp&url=$this->url";
		$signature = sha1($string);

		$signPackage = array(
			"appId"     => Yii::$app->params['wechat_authorization']['app_id'],
			"nonceStr"  => $nonceStr,
			"timestamp" => $timestamp,
			"url"       => $this->url,
			"signature" => $signature,
			"rawString" => $string
		);
		return $this->render('@frontend/views/WechatShare/WechatShare',[
			'signPackage'=>$signPackage,
			'shareInfo'=>[
				'title'	=>$this->title,
				'desc'	=>$this->desc,
				'link'	=>$this->link,
				'imgUrl'=>$this->imgUrl,
			],
		]);
	}
}